package com.jn.algorithm.day024.question76;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 江南大学1033190417
 * @date 2022/5/15 9:44
 */
public class Solution {
    Map<Character, Integer> ori = new HashMap<Character, Integer>();
    Map<Character, Integer> cnt = new HashMap<Character, Integer>();

    public String minWindow(String s, String t) {
        int tLen = t.length();
        for (int i = 0; i < tLen; i++) {
            char c = t.charAt(i);
            ori.put(c, ori.getOrDefault(c, 0) + 1);
        }
        int l = 0, r = -1;
        int len = Integer.MAX_VALUE, ansL = -1, ansR = -1;
        int sLen = s.length();
        while (r < sLen) {
            ++r;
            if (r < sLen && ori.containsKey(s.charAt(r))) {
                cnt.put(s.charAt(r), cnt.getOrDefault(s.charAt(r), 0) + 1);
            }
            while (check() && l <= r) {
                if (r - l + 1 < len) {
                    len = r - l + 1;
                    ansL = l;
                    ansR = l + len;
                }
                if (ori.containsKey(s.charAt(l))) {
                    cnt.put(s.charAt(l), cnt.getOrDefault(s.charAt(l), 0) - 1);
                }
                ++l;
            }
        }
        return ansL == -1 ? "" : s.substring(ansL, ansR);
    }

    public boolean check() {
        Iterator iter = ori.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Character key = (Character) entry.getKey();
            Integer val = (Integer) entry.getValue();
            if (cnt.getOrDefault(key, 0) < val) {
                return false;
            }
        }
        return true;
    }

    public String myMinWindow(String s, String t) {
        int left = 0, right = 0, bestLeft = 0, bestRight = 0, beatAns = Integer.MAX_VALUE;
        int ls = s.length(), lt = t.length();
        for (int i = 0; i < lt; i++) {
            char c = t.charAt(i);
            ori.put(c, ori.getOrDefault(c, 0) + 1);
        }
        while (right < ls) {
            char c = s.charAt(right);
            ++right;
            cnt.put(c, cnt.getOrDefault(c, 0) + 1);
            while (myCheck()) {
                if (right - left + 1 < beatAns) {
                    bestLeft = left;
                    bestRight = right;
                    beatAns = right - left + 1;
                }
                char cl = s.charAt(left);
                cnt.put(cl, cnt.getOrDefault(cl, 0) - 1);
                left++;
            }
        }
        return s.substring(bestLeft, bestRight);
    }

    public boolean myCheck() {
        Iterator<Map.Entry<Character, Integer>> iterator = ori.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Character, Integer> next = iterator.next();
            Character key = next.getKey();
            Integer value = next.getValue();
            if (cnt.getOrDefault(key, 0) < value) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.myMinWindow("a", "aa"));
    }

}
